#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
class Solution {
public:
    int maxEvents(vector<vector<int>>& events) {
        sort(events.begin(), events.end(), [](const vector<int>& left, const vector<int>& right) {
            return left[0] < right[0];
            });
        int n = events.size();
        priority_queue<int,vector<int>,greater<int>>queue;
        int res = 0; 
        int cur = 1;
        int index = 0;
        while (index < n||!queue.empty()) {
            while (index < n && events[index][0] == cur)
                queue.push(events[index++][1]);
            while (!queue.empty() && queue.top() < cur)
                queue.pop();
            if (!queue.empty()) {
                ++res;
                queue.pop();
            }
            ++cur;
        }
        return res;
    }
};

//int main() {
//    vector<vector<int>> events;
//    events.push_back({ 1, 2 });
//    events.push_back({ 2, 3 });
//    events.push_back({ 3, 4 });
//    events.push_back({ 1, 2 });
//    Solution sol;
//    sol.maxEvents(events);
//}